package com.yuting.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器的初始化和关闭
 * @author cyt
 * @date 2020/6/28
 */
public class ServerInitClose {
    private ServerSocket ss;

    private Socket s;

    private ObjectOutputStream oos;

    public ObjectOutputStream getOos() {
        return oos;
    }

    public ObjectInputStream getOis() {
        return ois;
    }

    private ObjectInputStream ois;

    /**
     * 初始化服务器
     * @throws IOException
     */
    public void serverInit() throws IOException {

        //创建ServerSocket
        ss = new ServerSocket(8099);
        System.out.println("等待客户端的连接。。。");
        s = ss.accept();
        System.out.println("客户端连接成功！");
        //初始化输出流和输入流
        ois = new ObjectInputStream(s.getInputStream());
        oos = new ObjectOutputStream(s.getOutputStream());
        System.out.println("服务器初始化成功！");
    }

    /**
     * 关闭和服务器相关的所有资源
     * @throws IOException
     */
    public void serverClose() throws IOException {
        oos.close();
        ois.close();
        s.close();
        ss.close();
        System.out.println("服务器关闭成功！");
    }
}
